package view
{
	import com.bit101.components.HBox;
	import com.bit101.components.ImageRadioBtn;
	import com.bit101.components.RadioButton;
	import com.bit101.components.VBox;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	public class ListItem extends VBox
	{
		public var currentEdit:EditItem;
		public var dic:Object;
		public function ListItem(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			dic = new Object;
		}
		private function addNewItem(obj:Object):Boolean{
			var editItme:EditItem = new EditItem(this);
			editItme.souce = obj;
			editItme.showAble = true;
			dic[obj.mark] = editItme;
			if(ModelLocator.getInstance().init == false){
				editItme.radio.selected = true;
				editItme.dispatch(1);
				editItme.onClick();
				editItme.onSelect();
				currentEdit = editItme;
				ModelLocator.getInstance().init = true;
				return true;
			}
			return false;
		}
		public function set source(value:XML):void{
			var editItme:EditItem;
			for each(editItme in dic){
				editItme.showAble = false;
			}
			var disAble:Boolean = false;
			for each(var xml:XML in value.type.img){
				var obj:Object = new Object;
				obj.name = String(xml.@name);
				obj.mark = String(xml.@mark);
				obj.price = int(xml.@price);
				obj.x = int(xml.@x);
				obj.y = int(xml.@y);
				obj.w = int(xml.@w);
				obj.h = int(xml.@h);
				obj.orgW = int(xml.@orgW);
				obj.orgH = int(xml.@orgH);
				
				if(dic.hasOwnProperty(obj.mark)){
					editItme = dic[obj.mark];
					editItme.data.price = obj.price;
					editItme.data.x = obj.x;
					editItme.data.y = obj.y;
					editItme.data.w = obj.w;
					editItme.data.h = obj.h;
					editItme.data.orgW = obj.orgW;
					editItme.data.orgH = obj.orgH;
					editItme.showAble = true;
					if(editItme.parent == null){
						this.addChild(editItme);
					}
					if(editItme == currentEdit){
						editItme.dispatch(-1);
						disAble = true;
					}
					if(editItme.radio.selected){
						editItme.add();
					}
					
				}else{
					if(addNewItem(obj)){
						disAble = true;
					}
				}
			}
			for each(editItme in dic){
				if(editItme.showAble == false){
					if(editItme.parent){
						editItme.radio.selected = false;
						editItme.dispatch(0);
						editItme.parent.removeChild(editItme);
					}
						
				}
			}
			if(disAble == false){
				for each(editItme in dic){
					if(editItme.showAble == true){
						if(editItme.radio.selected){
							editItme.onClick();
							editItme.onSelect();
						}
					}
				}
				
			}
			
		}
		
		public function clear():void{
			while(this.numChildren){
				this.removeChildAt(0);
			}
			dic = new Object;
			currentEdit = null
		}
		
		 
	}
}